﻿//"The contents of this file are subject to the Mozilla Public License
//Version 1.1 (the "License"); you may not use this file except in
//compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/

//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.

//The Original Code is TSharp.

//The Initial Developer of the Original Code is Mar3ek (http://mar3ek.wordpress.com).
//Portions created by Mar3ek are Copyright (C) 2010
//Mar3ek (http://mar3ek.wordpress.com). All Rights Reserved.

//Contributor(s): ______________________________________.

//Alternatively, the contents of this file may be used under the terms
//of the _____ license (the  "[___] License"), in which case the
//provisions of [______] License are applicable instead of those
//above.  If you wish to allow use of your version of this file only
//under the terms of the [____] License and not to allow others to use
//your version of this file under the MPL, indicate your decision by
//deleting  the provisions above and replace  them with the notice and
//other provisions required by the [___] License.  If you do not delete
//the provisions above, a recipient may use your version of this file
//under either the MPL or the [___] License."

using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace TSharp
{
    ///<summary>
    /// Class representing a tweet
    ///</summary>
    [Serializable]
    public class Tweet : ISerializable
    {
        private readonly TwitterUser _author;
        private readonly ulong _id;
        private readonly string _text;
        private readonly DateTime _time;
        private readonly ulong _retweetCount;
        private readonly bool _retweeted;

        ///<summary>
        /// Creates a new instance of the Tweet class
        ///</summary>
        ///<param name="tweetId">ID of the tweet</param>
        ///<param name="tweetText">Text of the tweet</param>
        ///<param name="tweetAuthor">Author of the tweet</param>
        ///<param name="createdAt">Time created</param>
        ///<param name="retweetCount">Number of times this status has been retweed</param>
        ///<param name="retweeted">Whether this status is a native retweet</param>
        public Tweet(ulong tweetId, string tweetText, TwitterUser tweetAuthor, DateTime createdAt, ulong retweetCount, bool retweeted)
        {
            _text = tweetText;
            _author = tweetAuthor;
            _id = tweetId;
            _time = createdAt;
            _retweetCount = retweetCount;
            _retweeted = retweeted;
        }

        /// <exception cref="ArgumentNullException"><paramref name="info" /> is <c>null</c>.</exception>
        protected Tweet(SerializationInfo info, StreamingContext context)
        {
            if (info == null) throw new ArgumentNullException("info");
            _text = (String) info.GetValue("text", typeof (String));
            _author = (TwitterUser) info.GetValue("author", typeof (TwitterUser));
            _id = (ulong) info.GetValue("id", typeof (ulong));
            _time = (DateTime) info.GetValue("time", typeof (DateTime));
            _retweeted = (bool) info.GetValue("retweeted", typeof (bool));
            _retweetCount = (ulong) info.GetValue("retweetcount", typeof (ulong));
        }

        ///<summary>
        /// Time created
        ///</summary>
        public DateTime Time
        {
            get { return _time; }
        }

        ///<summary>
        /// ID of the tweet
        ///</summary>
        public ulong Id
        {
            get { return _id; }
        }

        ///<summary>
        /// Text of the tweet
        ///</summary>
        public string Text
        {
            get { return _text; }
        }

        ///<summary>
        /// Author of the tweet
        ///</summary>
        public TwitterUser Author
        {
            get { return _author; }
        }

        ///<summary>
        /// Gets the number of times this status has been retweeted
        ///</summary>
        public ulong RetweetCount
        {
            get { return _retweetCount; }
        }

        ///<summary>
        /// Specifies whether this status is a native retweet
        ///</summary>
        public bool Retweeted
        {
            get { return _retweeted; }
        }

        #region ISerializable Members


        /// <summary>
        /// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.</param>
        /// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for this serialization.</param>
        /// <exception cref="T:System.Security.SecurityException">
        /// The caller does not have the required permission.
        /// </exception>
        /// <exception cref="ArgumentNullException"><paramref name="info" /> is <c>null</c>.</exception>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null) throw new ArgumentNullException("info");
            info.AddValue("text", _text);
            info.AddValue("author", _author);
            info.AddValue("id", _id);
            info.AddValue("time", _time);
            info.AddValue("retweetcount", _retweetCount);
            info.AddValue("retweeted", _retweeted);
        }

        #endregion
    }
}